<?php
/**
 * --------------------------------------------------
 * Copyright © 2018 PHP游戏项目学习交流群：820621745 版权所有
 *
 * 功能描述：
 *
 * 创建标识：Created by Zyc On 2018/8/15 14:46
 *
 * 修改标识：
 * 修改描述：
 *---------------------------------------------------
 */

use GatewayWorker\Lib\Gateway;

require_once __DIR__.'./../Common/DataBase.php';
require_once __DIR__.'./../Common/SessionMessage.php';

class UserDb
{

    /**
     * 通过token获取用户详情信息
     * @param $client_id
     * @param $token
     * @return bool
     */
    public static function getTokenToUserDetail($client_id,$token){
        $user_id=explode('_',$token)[0];
        if($user_id){
            $userDetail=DataBase::Table('GAME_USER')->where('user_id='.$user_id)->find();
            if($userDetail['token']==$token){
                SessionMessage::updateClientidToSession($client_id,'player_id',$user_id);
                SessionMessage::updateClientidToSession($client_id,'user_id',$user_id);
                SessionMessage::updateClientidToSession($client_id,'room_id',$userDetail['room_id']);
                SessionMessage::updateClientidToSession($client_id,'player_info',$userDetail);
                return $userDetail;
            }else{
                $message=ProtocolApi::returnMessage(ProtocolApi::$TOKEN_ERROR,'登录异常，请重新登录');
                Gateway::sendToClient($client_id, $message);
                return true;
            }
        }
        return true;
    }

    /**
     * 通过用户ID更新玩家数据库的Roomid
     * @param $userID
     * @param $roomID
     * @return bool
     */
    public static function updateUserTRoomId($userID,$roomID){
        DataBase::Table('GAME_USER')->where('user_id='.$userID)->update(['room_id'=>$roomID]);
        return true;
    }
}